<template>
  <div id="">
    <table border>
      <thead>

      </thead>
    </table>
  </div>
</template>
<script>
export default {
  name: "",
  data: () => ({

  }),
  methods: {
    init() {
      var record = result[0]
      console.log(record)
      var tHeader = [{ name: '店铺名', label: 'shopName' }];
      var tBody = [];
      var activityArr = [];
      // 初始化活动Array
      for (var i in record) {
        tBody.push({shopName: i});
        record[i].forEach(function (item) {
          var flag = false
          activityArr.forEach(function (activity) {
            if (activity.activityKey === item.activityKey) {
              flag = true;
              return false;
            }
          });
          if (!flag) {
            activityArr.push({
              activityKey: item.activityKey,
              activityName: item.activityName,
            });
          }
        });
      }

      // 初始化tHeader
      function setTheader(type) {
        activityArr.forEach(function (activity) {
          tHeader.push({
            name: activity.activityName,
            label: activity.activityKey + '_' + type,
            type: type,
          });
        });
      }
      // 活动投入(商品数)
      setTheader('itemCount');
      // 活动投入(商品天数)
      setTheader('shopContinueDays');
      // 活动效果(销额)
      setTheader('shopSaleAmountSum');
      // 活动效果(比平日提升)
      setTheader('advance');

      console.log(activityArr, tHeader);

      // 初始化表格数据
      function setTBody() {
        tBody.forEach(function (body) {
          tHeader.forEach(function (header) {
            if (header.label !== 'shopName') {
              body[header.label] = 0;
            }
          });
        });
        for (var i in record) {
          record[i].forEach(function (item) {
            tBody.forEach(function (body) {
              tHeader.forEach(function (header) {
                if (body.shopName === i && header.name === item.activityName) {
                  body[header.label] = item[header.type];
                }
              });
            });
          });
        }
      }
      setTBody();
      console.log(tBody)
    }
  },
  created() {
    //do something after creating vue instance
    this.init()
  }
}

const result = [{"美的美好专卖店":[{"activityKey":"juHuaSuan","activityName":"聚划算","shopName":"美的美好专卖店","partakeShops":2,"shopContinueDays":3,"shopKey":"1|1652490016","shopSaleAmountSum":617936.0001220703,"itemCount":39,"advance":0.3606255341621926},{"activityKey":"juHuaSuan-yuRe","activityName":"聚划算预热","shopName":"美的美好专卖店","partakeShops":2,"shopContinueDays":2,"shopKey":"1|1652490016","shopSaleAmountSum":-1799.0,"itemCount":27,"advance":147.41189549749862},{"activityKey":"otherActivities-cuxiaojia","activityName":"otherActivities-cuxiaojia","shopName":"美的美好专卖店","partakeShops":2,"shopContinueDays":30,"shopKey":"1|1652490016","shopSaleAmountSum":9995835.00012207,"itemCount":5,"advance":0.6047428754124342},{"activityKey":"otherActivities-kuanghuanjia","activityName":"otherActivities-kuanghuanjia","shopName":"美的美好专卖店","partakeShops":2,"shopContinueDays":6,"shopKey":"1|1652490016","shopSaleAmountSum":373258.0,"itemCount":7,"advance":-1.1169941434610915},{"activityKey":"otherActivities-youhuicuxiao","activityName":"otherActivities-youhuicuxiao","shopName":"美的美好专卖店","partakeShops":1,"shopContinueDays":15,"shopKey":"1|1652490016","shopSaleAmountSum":32788.0,"itemCount":4,"advance":-59.24955776503599},{"activityKey":"otherActivities-laidianjia","activityName":"otherActivities-laidianjia","shopName":"美的美好专卖店","partakeShops":2,"shopContinueDays":2,"shopKey":"1|1652490016","shopSaleAmountSum":58466.0,"itemCount":3,"advance":-3.5050969794410425}],"海尔官方旗舰店":[{"activityKey":"juHuaSuan","activityName":"聚划算","shopName":"海尔官方旗舰店","partakeShops":2,"shopContinueDays":16,"shopKey":"1|470168984","shopSaleAmountSum":1.1087816198150635E8,"itemCount":334,"advance":-0.09940171119699527},{"activityKey":"juHuaSuan-yuRe","activityName":"聚划算预热","shopName":"海尔官方旗舰店","partakeShops":2,"shopContinueDays":16,"shopKey":"1|470168984","shopSaleAmountSum":1.481378099987793E7,"itemCount":320,"advance":-7.228799994940535},{"activityKey":"manJian","activityName":"满减","shopName":"海尔官方旗舰店","partakeShops":1,"shopContinueDays":29,"shopKey":"1|470168984","shopSaleAmountSum":3.7923812E7,"itemCount":50,"advance":-4.825972856922529},{"activityKey":"otherActivities-cuxiaojia","activityName":"otherActivities-cuxiaojia","shopName":"海尔官方旗舰店","partakeShops":2,"shopContinueDays":30,"shopKey":"1|470168984","shopSaleAmountSum":4403505.000061035,"itemCount":69,"advance":-50.90452307955088},{"activityKey":"otherActivities-kuanghuanjia","activityName":"otherActivities-kuanghuanjia","shopName":"海尔官方旗舰店","partakeShops":2,"shopContinueDays":7,"shopKey":"1|470168984","shopSaleAmountSum":1.0499351697662354E7,"itemCount":95,"advance":-4.07946533086838},{"activityKey":"otherActivities-huodongcuxiao","activityName":"otherActivities-huodongcuxiao","shopName":"海尔官方旗舰店","partakeShops":1,"shopContinueDays":30,"shopKey":"1|470168984","shopSaleAmountSum":5316300.0,"itemCount":10,"advance":-41.99265032195055},{"activityKey":"otherActivities-huobaocuxiao","activityName":"otherActivities-huobaocuxiao","shopName":"海尔官方旗舰店","partakeShops":1,"shopContinueDays":21,"shopKey":"1|470168984","shopSaleAmountSum":1581010.6987609863,"itemCount":35,"advance":-100.19683501192891},{"activityKey":"yuShou","activityName":"预售","shopName":"海尔官方旗舰店","partakeShops":1,"shopContinueDays":7,"shopKey":"1|470168984","shopSaleAmountSum":0.0,"itemCount":39,"advance":0},{"activityKey":"taoQiangGou","activityName":"淘抢购","shopName":"海尔官方旗舰店","partakeShops":1,"shopContinueDays":2,"shopKey":"1|470168984","shopSaleAmountSum":331841.9997558594,"itemCount":7,"advance":-44.91780165957327},{"activityKey":"otherActivities-laidianjia","activityName":"otherActivities-laidianjia","shopName":"海尔官方旗舰店","partakeShops":2,"shopContinueDays":2,"shopKey":"1|470168984","shopSaleAmountSum":62916.0,"itemCount":3,"advance":-241.18728347488258}]}]
</script>
<style lang="scss" scoped>
</style>
